﻿using RealtReview.DataAccess.Entities;
using RealtyReview.Business.Helpers;
using RealtyReview.Business.Models.ViewModels;

namespace RealtyReview.Business.Mappings
{
    public static partial class EntityMap
    {
        public static Apartment ToApartment(this ApartmentViewModel model)
        {
            var json = Serializer.ToJson(model);
            return new Apartment
            {
                RoomsCount = model.RoomsCount,
                ApartmentType = model.Type,
                OptionalParameters = json,
            };
        }

        public static ApartmentViewModel ToApartmentViewModel(this Apartment entity)
        {
            var json = Serializer.ToObject<ApartmentViewModel>(entity.OptionalParameters);
            return new ApartmentViewModel
            {
                RoomsCount = entity.RoomsCount,
                Type = entity.ApartmentType,
                Entrance = (json != null) ? json.Entrance : null,
                FlatNo = (json !=null) ? json.FlatNo : null,
                Floor =  (json !=null) ? json.Floor : null,
                KitchenArea =  (json !=null) ? json.KitchenArea : null,
                LivingArea =  (json !=null) ? json.LivingArea : null,
                TotalArea =  (json !=null) ? json.TotalArea : null,
                TotalFloors =  (json !=null) ? json.TotalFloors : null,
                YearOfConstruction = (json != null) ? json.YearOfConstruction : null
            };
        }
    }
}